package com.group.transaction.controller;

import com.alibaba.fastjson.JSONObject;
import com.group.transaction.aspect.InMethod;
import com.group.transaction.domain.User;
import com.group.transaction.model.ResultJson;
import com.group.transaction.service.user.UserSrevice;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author YunXing Pang
 * @date 2020/3/15
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserSrevice userSrevice;

    @PostMapping("/login")
    @InMethod(apiName = "登录接口")
    public ResultJson login (@RequestBody User user) {
        return userSrevice.login(user);
    }

    @PostMapping("/register")
    @InMethod(apiName = "注册接口")
    public ResultJson register (@RequestBody User user) {
        return userSrevice.register(user);
    }

    @RequestMapping("/sendSmsCode")
    @InMethod(apiName = "发送短信验证码")
    public ResultJson sendSmsCode(String phone) {
        return userSrevice.sendSmsCode(phone);

    }


    /**
     * 编辑用户
     * @param user
     * @return
     */
    @PostMapping("/compileUser")
    @InMethod(apiName = "编辑用户")
    public ResultJson compileUser (@RequestBody User user) {
        return userSrevice.compileUser(user);
    }
    /**
     * 删除用户
     * @param user
     * @return
     */
    @GetMapping("/deleteUser")
    @InMethod(apiName = "删除用户")
    public JSONObject deleteUser (User user) {
        return userSrevice.deleteUser(user);
    }
    /**
     * 查询用户
     * @param user
     * @return
     */
    @GetMapping("/queryUserData")
    @InMethod(apiName = "查询用户")
    public JSONObject queryUserData (User user) {
        return userSrevice.queryUserData(user);
    }
    /**
     * 验证是否登录
     * @param
     * @return
     */
    @GetMapping("/verifyUserData")
    @InMethod(apiName = "验证是否登录")
    public JSONObject verifyUserData () {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        if(user != null) {
            return new JSONObject().fluentPut("result",1);
        }else {
            return new JSONObject().fluentPut("result",-1);
        }
    }
    /**
     * 查询当前用户宴请码
     * @param
     * @return
     */
    @GetMapping("/queryUserCode")
    @InMethod(apiName = "查询用户")
    public JSONObject queryUserCode () {
        return userSrevice.queryUserCode();
    }
    /**
     * 通过用户ID查询用户地址
     * @param
     * @return
     */
    @GetMapping("/queryUserAddressByUserId")
    @InMethod(apiName = "查询地址")
    @RequiresPermissions({"file:read", "write:aFile.txt"} )
    public JSONObject queryUserAddressByUserId () {
        return userSrevice.queryUserAddressByUserId();
    }
    /**
     * 新增发货地址
     * @param address
     * @return
     */
    @GetMapping("/addSendAddress")
    @InMethod(apiName = "添加地址")
    public ResultJson addSendAddress ( String address) {
        return userSrevice.addSendAddress(address);
    }
    /**
     * 根据用户ID查询用户信息
     * @param userId
     * @return
     */
    @GetMapping("/queryUserDataByUserId")
    @InMethod(apiName = "根据用户ID查询用户信息")
    public ResultJson queryUserDataByUserId ( String userId) {
        Long id = Long.parseLong(userId);
        return userSrevice.queryUserDataByUserId(id);
    }
    /**
     * 根据用户ID查询用户信息
     * @return
     */
    @GetMapping("/queryUserDataById")
    @InMethod(apiName = "根据用户ID查询用户信息")
    public ResultJson queryUserDataById () {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        return userSrevice.queryUserDataByUserId(user.getId());
    }
    @RequestMapping("/sendLogistics")
    @InMethod(apiName = "手机号码和身份证后四位寄件")
    public ResultJson sendLogistics(User user) {
        return userSrevice.sendLogistics(user);

    }
    /**
     * 通过用户ID查询用户地址
     * @param
     * @return
     */
    @GetMapping("/queryAddressByUserId")
    @InMethod(apiName = "查询地址")
    @RequiresPermissions({"file:read", "write:aFile.txt"} )
    public ResultJson queryAddressByUserId (String userId) {
        return userSrevice.queryAddressByUserId(userId);
    }
    /**
     * 新增发货地址
     * @param user
     * @return
     */
    @GetMapping("/addUserAddress")
    @InMethod(apiName = "添加地址")
    public ResultJson addUserAddress ( User user) {
        return userSrevice.addUserAddress(user);
    }
    /**
     * 根据用户ID查询用户信息
     * @return
     */
    @GetMapping("/queryLoginUserData")
    @InMethod(apiName = "根据用户ID查询用户信息")
    public ResultJson queryLoginUserData () {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        return ResultJson.builder().code(1).message("成功").result(user).build();
    }
}
